/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SubsitemaAgenda;

import clienteSandraParada.Mensajeria;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author hernaneleazar
 */
public class DialogoAgregarPrestacion extends javax.swing.JDialog {

    DialogoPagoBonoSinProg dpb;
    public DialogoAgregarPrestacion(java.awt.Frame parent, boolean modal, DialogoPagoBonoSinProg dpb) {
        super(parent, modal);
        initComponents();
        setIconImage(new ImageIcon(getClass().getResource("/GUI/Img/logo2.png")).getImage());
        this.llenarTabla();
        this.dpb = dpb;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botonCancelar = new javax.swing.JButton();
        botonAgregar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaPrestaciones = new javax.swing.JTable();
        spinnerCantidad = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        dinero1 = new componentes.Dinero();
        jLabel3 = new javax.swing.JLabel();
        etiquetaMensajeria = new javax.swing.JLabel();
        etiquetaMensajeria1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        botonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos16X16/Cancelar.png"))); // NOI18N
        botonCancelar.setText("Cancelar");
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        botonAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos16X16/Agregar.png"))); // NOI18N
        botonAgregar.setText("Agregar");
        botonAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAgregarActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos prestación"));

        jLabel1.setText("Cantidad:");

        tablaPrestaciones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Prestación"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tablaPrestaciones);
        tablaPrestaciones.getColumnModel().getColumn(0).setResizable(false);
        tablaPrestaciones.getColumnModel().getColumn(1).setResizable(false);

        spinnerCantidad.setModel(new javax.swing.SpinnerNumberModel(1, 1, 100, 1));

        jLabel2.setText("Valor:");

        jLabel3.setText("*");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(spinnerCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(47, 47, 47)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dinero1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addGap(0, 305, Short.MAX_VALUE))
                    .addComponent(jScrollPane1))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(spinnerCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addComponent(dinero1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        etiquetaMensajeria.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        etiquetaMensajeria.setText("Área de mensajería");

        etiquetaMensajeria1.setText("   * Campos obligatorios");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(etiquetaMensajeria, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(etiquetaMensajeria1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(botonAgregar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonCancelar)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(botonCancelar)
                        .addComponent(botonAgregar))
                    .addComponent(etiquetaMensajeria1))
                .addGap(18, 18, 18)
                .addComponent(etiquetaMensajeria)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAgregarActionPerformed
        if(!dinero1.getDinero().getText().isEmpty()){
            int i = tablaPrestaciones.getSelectedRow();        
            if(i != -1){
                this.dispose();
                service.CodigosPrestaciones spm = (service.CodigosPrestaciones)cargarCodigoPrestacion(tablaPrestaciones.getValueAt(i, 0).toString());
                if(dpb!=null){
                    dpb.agregarPrestacionTabla(Integer.parseInt(spinnerCantidad.getValue().toString()), spm.getId(), spm.getNombrePrestacion(), dinero1.getDinero().getText());
                    dpb.actualizarTotal();
                }           
            }
            else{
                this.etiquetaMensajeria.setText("Debe seleccionar una prestación");
                this.etiquetaMensajeria.setForeground(Color.red);
            } 
        }
        else{
            this.cambiarEtiquetaMensajeria(Mensajeria.mensajeCamposObligatorios(), Color.red);
        }
                 
    }//GEN-LAST:event_botonAgregarActionPerformed

    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_botonCancelarActionPerformed

    public void agregarFila(){
        DefaultTableModel temp = (DefaultTableModel) tablaPrestaciones.getModel();
        Object nuevo[]= {"","",""};
        temp.addRow(nuevo);
    }
        
    public void cambiarEtiquetaMensajeria(String mensaje, Color color) {
        etiquetaMensajeria.setText(mensaje);
        etiquetaMensajeria.setForeground(color);
    }
    
    public void limpiarTabla(){
        DefaultTableModel temp = (DefaultTableModel) tablaPrestaciones.getModel();
        for(int i = tablaPrestaciones.getRowCount() -1; i >= 0; i--){
            temp.removeRow(i);
        } 
    }
    
    public void llenarTabla(){
        this.limpiarTabla();
        java.util.List<service.CodigosPrestaciones> prestaciones =  listarCodigosPrestaciones();
        int i = 0;
        for(service.CodigosPrestaciones p : prestaciones){
            this.agregarFila();
            tablaPrestaciones.setValueAt(p.getId(), i, 0);
            tablaPrestaciones.setValueAt(p.getNombrePrestacion(), i, 1);
            i++;
        }
    }
     public void normalizarMensajeria() {
        etiquetaMensajeria.setText("Área de mensajería");
        etiquetaMensajeria.setForeground(Color.black);
    }
    
    public void ocultarColumnaID(){
        tablaPrestaciones.getColumnModel().getColumn(0).setMaxWidth(0);
        tablaPrestaciones.getColumnModel().getColumn(0).setMinWidth(0);
        tablaPrestaciones.getColumnModel().getColumn(0).setPreferredWidth(0);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAgregar;
    private javax.swing.JButton botonCancelar;
    private componentes.Dinero dinero1;
    private javax.swing.JLabel etiquetaMensajeria;
    private javax.swing.JLabel etiquetaMensajeria1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner spinnerCantidad;
    private javax.swing.JTable tablaPrestaciones;
    // End of variables declaration//GEN-END:variables

    private static Object cargarCodigoPrestacion(java.lang.String id) {
        service.PrestacionMedicaGeneralWSService service = new service.PrestacionMedicaGeneralWSService();
        service.PrestacionMedicaGeneralWS port = service.getPrestacionMedicaGeneralWSPort();
        return port.cargarCodigoPrestacion(id);
    }

    private static java.util.List<service.CodigosPrestaciones> listarCodigosPrestaciones() {
        service.PrestacionMedicaGeneralWSService service = new service.PrestacionMedicaGeneralWSService();
        service.PrestacionMedicaGeneralWS port = service.getPrestacionMedicaGeneralWSPort();
        return port.listarCodigosPrestaciones();
    }    
}
